package com.dnjn.happiness.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.DateUtils;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.datascope.annotation.DataScope;
import com.dnjn.happiness.survey.api.enums.DimensionalityGrade;
import com.dnjn.happiness.survey.domain.Question;
import com.dnjn.happiness.survey.domain.SurveyRecord;
import com.dnjn.happiness.survey.domain.vo.AnswerVO;
import com.dnjn.happiness.survey.domain.vo.ExportSurveyRecord;
import com.dnjn.happiness.survey.domain.vo.SurveyAnswerTypeVo;
import com.dnjn.happiness.survey.domain.vo.SurveyRecordVO;
import com.dnjn.happiness.survey.domain.vo.SurveyUserVO;
import com.dnjn.happiness.survey.mapper.SurveyRecordMapper;
import com.dnjn.happiness.survey.service.IAnswerService;
import com.dnjn.happiness.survey.service.ISurveyRecordService;
import com.dnjn.happiness.system.api.RemoteDeptService;
import com.dnjn.happiness.system.api.RemoteRoleService;
import com.dnjn.happiness.system.api.domain.SysDept;
import com.dnjn.happiness.system.api.domain.SysRole;
import com.dnjn.happiness.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 参加问卷的人记录Service业务层处理
 *
 * @author happiness
 * @date 2023-10-23
 */
@Service
public class SurveyRecordServiceImpl extends ServiceImpl<SurveyRecordMapper, SurveyRecord> implements ISurveyRecordService {
    @Autowired
    SurveyRecordMapper recordMapper;

    @Autowired
    private IAnswerService answerService;

    @Autowired
    private RemoteRoleService roleService;

    @Autowired
    private RemoteDeptService deptService;

    //去评价
    @Override
    public int goToPing(SurveyRecord domain) {

        domain.setLikeCounts(DimensionalityGrade.getValue(domain.getAppraiseGrade()).getValue());

        UpdateWrapper updateWrapper = new UpdateWrapper();

        updateWrapper.eq("survey_record_id", domain.getSurveyRecordId());

        updateWrapper.set("date_appraise", DateUtils.getNowDate());
        updateWrapper.set("like_counts", domain.getLikeCounts());
        updateWrapper.set("appraiser", domain.getAppraiser());
        updateWrapper.set("answer_appraise", domain.getAnswerAppraise());
        updateWrapper.set("appraise_grade", domain.getAppraiseGrade());

        return baseMapper.update(null, updateWrapper);


    }


    @Override
    public int getSurveyRecordNotJoin(Long userId, String time) {
        return recordMapper.getSurveyRecordNotJoin(userId, time);
    }


    @Override
    @DataScope(deptAlias = "u")
    public Page<SurveyRecord> getPage(Page page, SurveyRecordVO surveyRecordVO) {
        return baseMapper.getPage(page, surveyRecordVO);
    }

    @Override
    public List<SysUser> goToTheDetailsView(Long surveyId) {

        return baseMapper.goToTheDetailsView(surveyId);
    }

    @Override
    public List<SurveyRecord> forSurveyDetailsSeeFeedbackBySurveyIdAndLeaderId(Long surveyId, Long leaderId) {
        return baseMapper.forSurveyDetailsSeeFeedbackBySurveyIdAndLeaderId(surveyId, leaderId);
    }


    @Override
    public List<SurveyRecordVO> getPage1(SurveyRecordVO surveyRecordVO) {
        return baseMapper.getPage1(surveyRecordVO);
    }

    @Override
    public List<SurveyUserVO> selectGrade(Long surveyId) {

        return baseMapper.selectGrade(surveyId);
    }

    @Override
    public SurveyUserVO queryRecord(Long surveyRecord) {
        return baseMapper.queryRecord(surveyRecord);
    }

    @Override
    public Page<SurveyRecord> goToTheSurveyDetailsView(Page<SurveyRecord> page, Long surveyId, String SurveyStatus) {

        return baseMapper.goToTheSurveyDetailsView(page, surveyId, SurveyStatus);
    }

    @Override
    public Page<SurveyRecord> answerDetailsFilterTheCorrespondingScoreRange(Page<SurveyRecord> page, Long surveyId, Integer startScore, Integer endScore) {
        return baseMapper.answerDetailsFilterTheCorrespondingScoreRange(page, surveyId, startScore, endScore);
    }

    @Override
    public List<SurveyRecord> queryRecordX(Long surveyId) {

        return baseMapper.queryRecordX(surveyId);
    }

    @Override
    public List<SurveyRecord> selectGradeX(Long surveyId) {
        return baseMapper.selectGradeX(surveyId);
    }

    @Override
    public List<SurveyUserVO> serchRecoed(AnswerVO answerVO) {
        return baseMapper.serchRecoed(answerVO);
    }

    @Override
    public Double selectGradeByX(Long userId, String time, double recordScore, double x, double y) {
        return baseMapper.selectGradeByXY(userId, time, recordScore, x, y);
    }


    @Override
    public Integer getSurveyRecordNotJoinNew(Long userId) {
        return baseMapper.getSurveyRecordNotJoinNew(userId);
    }

    @Override
    public List<SurveyUserVO> screeningSubmit(AnswerVO answerVO) {
        List<SurveyUserVO> vos = baseMapper.serchRecoed(answerVO);//
        vos.stream().forEach(vo -> {
            getRoleAndDept(vo);
        });
        if (null != answerVO.getQuestionList() && answerVO.getQuestionList().size() > 0) {
            List<SurveyUserVO> surveyUserVOList = answerService.screeningSubmit(answerVO);

            surveyUserVOList.stream().forEach(surveyUserVO -> {
                getRoleAndDept(surveyUserVO);
            });
            List<Question> questionList = answerVO.getQuestionList();
            ArrayList<SurveyUserVO> vo = new ArrayList<>();
            Set<Long> set = new HashSet<Long>();
            if (null != questionList) {
                for (SurveyUserVO u : surveyUserVOList) {
                    Set<SurveyUserVO> voSet = new HashSet<SurveyUserVO>();
                    for (Question q : questionList) {
                        if (q.getQuestionId().equals(u.getQuestionId()) && u.getAnswer().contains(q.getAnswer())) {
                            if (!set.contains(u.getUserId())) {
                                set.add(u.getUserId());
                                vo.add(u);
//                                vo.remove(vo.size()-1);
                            }
                            continue;
                        }
                    }

                }
                return vo;
            } else {
                return surveyUserVOList;
            }
        }
        return vos;
    }
    @Override
    public List<SurveyRecord> exportSurveyId(Long surveyId) {
        return baseMapper.exportSurveyId(surveyId);
    }

    @Override
    public List<ExportSurveyRecord> exportSurveyRecord(Long surveyId) {
        return baseMapper.exportSurveyRecord(surveyId);
    }

    private void getRoleAndDept(SurveyRecord surveyRecord) {
        Long userId = surveyRecord.getUserId();
        // 判断角色
        if (null != userId) {
            List<SysRole> sysRoles = new ArrayList<>();
            R<List<SysRole>> result = roleService.queryRole(userId, SecurityConstants.INNER);
            if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                sysRoles = result.getData();
            }
            sysRoles.stream().forEach(sysRole -> {
                if (null != sysRole.getRoleKey() && sysRole.getRoleKey().contains("common")) {
                    surveyRecord.setRole("1");//普通
                }
                if (null != sysRole.getRoleKey() && !sysRole.getRoleKey().contains("common")) {
                    surveyRecord.setRole("2");//高亮
                }
            });
        }
        Long deptId = surveyRecord.getDeptId();
        if (null != deptId) {
            SysDept sysDept = new SysDept();
            R<SysDept> remoteResult = deptService.getInfoById(deptId, SecurityConstants.INNER);
            if (StringUtils.isNotNull(remoteResult) && StringUtils.isNotNull(remoteResult.getData())) {
                sysDept = remoteResult.getData();
            }
            surveyRecord.setSysDept(sysDept);
        }
    }

    @Override
    public List<SurveyAnswerTypeVo> getRecordByUserId(Long userId, String tikuTagsType) {
        return baseMapper.getRecordByUserId(userId, tikuTagsType);
    }

    @Override
    public List<SurveyRecord> queryRecordBySurveyIdIsNot(long surveyId) {
        return baseMapper.queryRecordBySurveyIdIsNot(surveyId);
    }


}
